<?php

namespace app\admin\validate;

use app\common\validate\tysj\Store as ParentValidate;

class Store extends ParentValidate
{
	/**
     * 验证规则
     */
    protected $rule = [
        'id'          => 'require',
        'examine'     => 'require|isOptions:StoreExamine',
        'back_url'    => 'max:200|back_url',
        'sub_mchid'   => 'max:20|sub_mchid'
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'id.require'          => '店铺ID不能为空',
        'examine.require'     => '审核状态不能为空',
        'examine.isOptions'   => '审核状态不合法',
        'back_url.max'        => '扫脸url不能超过200个字符',
        'back_url.back_url'   => '扫脸url不能为空',
        'sub_mchid.max'       => '子商户号不能超过20个字符',
        'sub_mchid.sub_mchid' => '子商户号不能为空'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'save' => ['id', 'examine', 'back_url', 'sub_mchid'],
    ];

    //验证扫脸url
    protected function back_url($value, $rule, $data = []) {
        $examine = loadEnum('StoreExamine');
        if($examine->facescan() == $data['examine']) {
            return !!$value;
        }
        return true;
    }

    //验证子商户号
    protected function sub_mchid($value, $rule, $data = []) {
        $examine = loadEnum('StoreExamine');
        if($examine->complete() == $data['examine']) {
            return !!$value;
        }
        return true;
    }

    //重构验空函数
    protected function require($value, $rule, $data = []) {
        $result = empty($value) && $value !== 0 && $value !== '0';
        if(!$result) {
            return true;
        }
        if($rule) {
            return $data[$rule];
        } else {
            return false;
        }
    }
}